<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class UserController extends Controller
{
    //身份信息
    public function information(Request $request)
    {
        $u_id = $request->u_id;

        $select = ['name', 'id_card', 'id_card_positive', 'id_card_contrary', 'identity', 'alipay_account', 'alipay_name',
            'bank_name', 'bank_number', 'bank_type', 'opening_bank', 'partner_name', 'partner_phone', 'partner_address', 'serial_number'];

        $user = DB::table('users')->where('id', $u_id)->select($select)->first();

        return ['code' => 1, 'message' => '请求成功', 'data' => $user];
    }

    //编辑基本信息
    public function UpdateBasicInformation(Request $request)
    {
        //获取用户信息
        $u_id = $request->u_id;
        $name = $request->post('name');
        $id_card = $request->post('id_card');
        $id_card_positive = $request->post('id_card_positive');
        $id_card_contrary = $request->post('id_card_contrary');

        $user = DB::table('users')->where('id', $u_id)->first();

        $update = [];
        if($name != $user->name) {
            $update['name'] = $name;
        }
        if($id_card != $user->id_card) {
            $update['id_card'] = $id_card;
        }
        if(!empty($id_card_positive)) {
            if($id_card_positive != $user->id_card_positive) {
                $update['id_card_positive'] = $id_card_positive;
            }
        }
        if(!empty($id_card_contrary)) {
            if($id_card_contrary != $user->id_card_contrary) {
                $update['id_card_contrary'] = $id_card_contrary;
            }
        }

        if(!empty($update)) {
            $res = DB::table('users')->where('id', $u_id)->update($update);
            if(!$res) {
                return ['code' => 0, 'message' => '修改失败', 'data' => ''];
            }
        }
        return ['code' => 1, 'message' => '修改成功', 'data' => ''];
    }

    //修改用户收款信息
    public function UpdateGatheringInformation(Request $request)
    {
        //获取信息
        $u_id  = $request->u_id;
        $alipay_account  = $request->post('alipay_account');
        $alipay_name  = $request->post('alipay_name');
        $bank_name  = $request->post('bank_name');
        $bank_number  = $request->post('bank_number');
        $bank_type  = $request->post('bank_type');
        $opening_bank  = $request->post('opening_bank');

        $user = DB::table('users')->where('id', $u_id)->first();

        $update = [];
        if($alipay_account != $user->alipay_account) {
            $update['alipay_account'] = $alipay_account;
        }
        if($alipay_name != $user->alipay_name) {
            $update['alipay_name'] = $alipay_name;
        }
        if($bank_name != $user->bank_name) {
            $update['bank_name'] = $bank_name;
        }
        if($bank_number != $user->bank_number) {
            $update['bank_number'] = $bank_number;
        }
        if($bank_type != $user->bank_type) {
            $update['bank_type'] = $bank_type;
        }
        if($opening_bank != $user->opening_bank) {
            $update['opening_bank'] = $opening_bank;
        }

        if(!empty($update)) {
            $res = DB::table('users')->where('id', $u_id)->update($update);
            if(!$res) {
                return ['code' => 0, 'message' => '修改失败', 'data' => ''];
            }
        }
        return ['code' => 1, 'message' => '修改成功', 'data' => ''];
    }

    //修改用户收货地址
    public function UpdateShippingAddress(Request $request)
    {
        //获取信息
        $u_id  = $request->u_id;
        $partner_name  = $request->post('partner_name');
        $partner_phone  = $request->post('partner_phone');
        $partner_address  = $request->post('partner_address');

        $user = DB::table('users')->where('id', $u_id)->first();

        $update = [];
        if($partner_name != $user->partner_name) {
            $update['partner_name'] = $partner_name;
        }
        if($partner_phone != $user->partner_phone) {
            $update['partner_phone'] = $partner_phone;
        }
        if($partner_address != $user->partner_address) {
            $update['partner_address'] = $partner_address;
        }

        if(!empty($update)) {
            $res = DB::table('users')->where('id', $u_id)->update($update);
            if(!$res) {
                return ['code' => 0, 'message' => '修改失败', 'data' => ''];
            }
        }
        return ['code' => 1, 'message' => '修改成功', 'data' => ''];
    }
}
